package com.gzgy.ustore.api.dao.impl;

import com.gzgy.ustore.api.common.jdbc.StatementParameter;
import com.gzgy.ustore.api.dao.AppInfoDao;
import com.gzgy.ustore.api.model.ApkListByWebModel;
import com.gzgy.ustore.api.model.ApkListModel;
import com.ustore.model.AppInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class AppInfoDaoImpl extends AbstractBaseDao<AppInfo> implements AppInfoDao {
    @Override
    public AppInfo getByApkFileId(Integer apkFileId) {
        String sql ="SELECT * FROM "+this.tableName+" WHERE APK_FILE_ID = ? ";
        StatementParameter parameter = new StatementParameter();
        parameter.setInt(apkFileId);

        return this.query(sql,AppInfo.class,parameter);
    }

    @Override
    public List<ApkListModel> getApkForAppDvc() {
        StringBuffer sb = new StringBuffer();
        sb.append(" SELECT APP_LOGO_PATH,APP_NAME,APP_PACKAGE_NAME,APP_PATH,APP_VERSION_NAME,");
        sb.append(" DETAIL_DESC,APP_SIZE,UPDATE_DESC,CREATED_TIME as date,DEVELOPER AS Author ");
        sb.append(" FROM app_info where IS_DELETED =0 and EXPIRED=0 ");
        return this.queryForList(sb.toString(),ApkListModel.class);
    }

    @Override
    public int apkListCount(String companyId, String appCode, String dvcType) {
        StatementParameter parameter = new StatementParameter();
        parameter.setString(companyId);
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT count(*) FROM apk_file_record afr   ");
        sb.append("  INNER JOIN app_info ai on ai.APK_FILE_ID = afr.ID and ai.IS_DELETED = 0  ");
        sb.append("  where ai.COMPANY_ID = ?   ");
        if(StringUtils.isNotEmpty(appCode)){
            sb.append(" and ai.APP_CODE = ? ");
            parameter.setString(appCode);
        }
        if(StringUtils.isNotEmpty(dvcType)){
            sb.append(" and ai.DEVICE_TYPE = ? ");
            parameter.setString(dvcType);
        }
        sb.append(" and ai.EXPIRED = 0 and afr.IS_DELETED = 0 ;");
        return this.queryForInt(sb.toString(),parameter);
    }

    @Override
    public List<ApkListByWebModel> getApkListByWeb(String companyId,String index,String page,String appCode,String dvcType) {
        StatementParameter parameter = new StatementParameter();
        parameter.setString(companyId);

        StringBuffer sb = new StringBuffer();
        sb.append("SELECT ai.APK_FILE_ID,ai.APP_CODE,ai.APP_LOGO_PATH,ai.APP_NAME, ");
        sb.append(" ai.APP_PACKAGE_NAME,afr.APP_SIZE,afr.APP_VERSION,afr.APP_VERSION_NAME,") ;
        sb.append(" ai.DETAIL_DESC,ai.SUMMARY_DESC,ai.OS_INFO,ai.UPDATE_DESC,ai.DEVELOPER,ai.DEVICE_TYPE,ai.UPDATED_BY ");
        sb.append(" FROM apk_file_record afr  ");
        sb.append(" INNER JOIN app_info ai on ai.APK_FILE_ID = afr.ID and ai.IS_DELETED = 0 ");
        sb.append(" where ai.COMPANY_ID = ?  ");
        if(StringUtils.isNotEmpty(appCode)){
            sb.append(" and ai.APP_CODE = ? ");
            parameter.setString(appCode);
        }
        if(StringUtils.isNotEmpty(dvcType)){
            sb.append(" and ai.DEVICE_TYPE = ? ");
            parameter.setString(dvcType);
        }
        sb.append("  and ai.EXPIRED = 0 and afr.IS_DELETED = 0 limit ?, ?; ");
        parameter.setInt(NumberUtils.toInt(index)*NumberUtils.toInt(page));
        parameter.setInt(NumberUtils.toInt(page));

        return this.queryForList(sb.toString(),ApkListByWebModel.class,parameter);
    }


}
